﻿using System;
using System.Collections.Generic; 
using System.Threading.Tasks; 
using Microsoft.AspNetCore.Mvc;
using Blog.IServices;
using Model.Models; 
using Microsoft.AspNetCore.Authorization; 
using Blog.Common.Redis;
using System.Linq.Expressions;
using AutoMapper;
using Blog.Core3.Model.VeiwModels;
using Model;
using System.Linq;

namespace Blog.Core3.Controllers
{
    /// <summary>
    ///  博客控制器
    /// </summary>

    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BlogController : Controller
    {
        readonly IArticleInfoServices articleInfoServices; //服务
      
    
        public BlogController(IArticleInfoServices _articleInfoServices)
        {
            articleInfoServices = _articleInfoServices; 
        }

        /// <summary>
        /// 获取所有 每个博客的部分内容显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<object> GetByAccountBlog(decimal account, int page, int size)
        {
           var obj= await  articleInfoServices.GetByAccountBlog(account, page, size); 
            return Json(obj);
        }

        /// <summary>
        /// 添加博客
        /// </summary>
        /// <param name="articleInfo"></param>
        /// <returns></returns>
       // [Authorize(Policy = "Admin")]
        [HttpPost]
        public async Task<object> PostAddAccountBlog(ArticleInfo articleInfo)
        {
            int execute = await articleInfoServices.Add(articleInfo);
            var json = execute > 0 ?
            new
            {
                Msg = "数据添加成功",
                Success = true
            }
            :
            new
            {
                Msg = "数据添加失败",
                Success = false
            };
            return Json(json);

        }



        /// <summary>
        ///  测试添加
        /// </summary>
        /// <param name="num"></param>
        /// <param name="UserAccount"></param>
        /// <returns></returns>
        //[Authorize(Policy = "Admin")]
        [HttpPost]
        public async Task<object> PostAddAccountBlogs(decimal UserAccount, int num = 10)
        {
            List<ArticleInfo> articleInfos = new List<ArticleInfo>();
            
            for (int i = 0; i < num; i++)
            {
                string Guids = Guid.NewGuid().ToString();
                articleInfos.Add(new ArticleInfo
                {
                    ArticleAbstract = $"这是文章的随机摘要:{Guids}",
                    ArticleBody = $"这是文章的随机摘要:{Guids}",
                    ArticleCreateDate = i,
                    ArticleTitle = $"这是文章的随机摘要:{Guids}",
                    UserAccount = UserAccount.ToString(),
                    ArticleID = Guids,
                    ArticleIsRelease = 1 
                }); 
            }
            int execute = await articleInfoServices.Add(articleInfos);
            var json = execute == articleInfos.Count ?
            new
            {
                Msg = "模拟数据已插入数据库",
                Success = true
            }
            :
            new
            {
                Msg = "模拟数据插入数据库失败",
                Success = false
            };
            return Json(json);

        }




        /// <summary>
        /// 删除文章
        /// </summary>
        /// <param name="ArticleID">文章ID</param> 
        /// <returns></returns>
        [Authorize(Policy = "Admin")]
        [HttpDelete]
        public async Task<object> DeleteAccountBlog(object ArticleID)
        {
            bool execute = await articleInfoServices.DeleteById(ArticleID);

            var json = execute ?
          new
          {
              Msg = "数据删除成功",
              Success = true
          }
          :
          new
          {
              Msg = "数据删除失败",
              Success = false
          };
            return Json(json);

        }



        /// <summary>
        /// 恢复删除文章  
        /// </summary>
        /// <param name="ArticleID">文章ID</param> 
        /// <returns></returns>
        [Authorize(Policy = "Client")]
        [HttpPut]
        public async Task<object> PutAccountBlog(object ArticleID)
        {
            bool execute = await articleInfoServices.DeleteById(ArticleID);

            var json = execute ?
          new
          {
              Msg = "数据删除成功",
              Success = true
          }
          :
          new
          {
              Msg = "数据删除失败",
              Success = false
          };
            return Json(json);

        }
    }
}